<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>别踩白块</title>
    <link rel="stylesheet" href="wc.css">
</head>
<body>
    <!--字体-->
    <div class="game-style">
        别踩白块儿
    </div>
    <div class="game-over" id="gameOver"></div>
    <!--分数-->
    <span class="game-score" id="gameScore">分数：</span>
    <!--游戏面板-->
    <button id="Button">游戏开始</button>
    <div class ="game-model">
        <table id="Table">
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </div>
    <script >
        var Table = document.getElementById('Table');
        var html = document.documentElement;
        var Tbody = Table.firstElementChild;
        var index = 0;
        var Button = document.getElementById('Button');
        var gameScore = document.getElementById('gameScore');
        var gameOver = document.getElementById('gameOver');
        //点击游戏开始才能玩
        Button.addEventListener('click',function(){
            Button.style.display = 'none';
            html.addEventListener('keypress',ISgame)
        //
    
        function ISgame(event){
            var num;
            //随机三行黑色
            for(i=0;i<=3;i++){
                if(Tbody.lastElementChild.children[i].style.background =='black'){
                    num = i+1;
                }
            }

            //键盘事件
            if(event.key ==='h'&& num==1 ||event.key ==='j'&& num==2 || event.key ==='k'&& num==3 || event.key ==='l'&& num==4 
            || event.key ==='H'&& num==1 ||event.key ==='J'&& num==2 || event.key ==='K'&& num==3 || event.key ==='L'&& num==4
            ){
                //新增
                var first = Table.firstElementChild.firstElementChild;
                var trobj = document.createElement('tr');
                var tdobj1 = document.createElement('td');
                var tdobj2 = document.createElement('td');
                var tdobj3 = document.createElement('td');
                var tdobj4 = document.createElement('td');
                trobj.appendChild(tdobj1);
                trobj.appendChild(tdobj2);
                trobj.appendChild(tdobj3);
                trobj.appendChild(tdobj4);
                trobj.children[parseInt(Math.random()*4)].style.background = 'black';
                Tbody.insertBefore(trobj,first);
                
                //分数增加
                index++;
                gameScore.innerHTML = "分数：" + parseInt(index+"");
                console.log(index)
                //document.body.appendchild(add);
                //删除
                Tbody.removeChild(Tbody.lastElementChild);
            }else{
                    index--;
                    gameScore.innerHTML = "分数：" + parseInt(index+"");
                    console.log(index)
                }
            console.log(index)
        }
        });
        var Button = document.getElementById('Button');
        (function Color(){
            for(i=0;i<3;i++){
                var num1 = parseInt(Math.random()*4);
                Tbody.children[i].children[num1].style.background = 'black';
            }
        })();

    </script>
</body>
</html>